package com.dhcc.bpm.modules.scf.controller.HomePageController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dhcc.bpm.common.utils.StringUtils;
import com.dhcc.bpm.modules.scf.entity.HomePage.*;
import com.dhcc.bpm.modules.scf.service.HomePageService.IScfHomePageService;
import com.dhcc.bpm.modules.scf.util.UnitConverter;
import com.dhcc.bpm.modules.scf.vo.HomePageVO.ScfHomePageVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

/**
 * 首页数据展示控制器
 */
@RestController
@AllArgsConstructor
public class ScfHomePageController extends BladeController {
    private IScfHomePageService scfHomePageService;
    private UnitConverter unitConverter;

    /**
     * 首页数据展示部分
     *
     * @return
     */
    @GetMapping("/scfGetHomePageInfo")
    // @Cacheable(value = "homePageCache", key = "'HomePageInfo'")
    public R<ScfHomePageVO> HomePageInfo(String orgCode) {
        if (StringUtils.isEmpty(   orgCode) ) {
            return  R.fail(400,"公司编码不能为空") ;
        }

        ScfHomePageVO scfHomePage = new ScfHomePageVO();
        // 查询各类企业数量
        List<ScfSubjectCount> scfSubjectCounts = scfHomePageService.selectSubjectCount(orgCode.split(",") );
        scfHomePage.setScfSubjectCount(scfSubjectCounts);
        // 查询近30天成交信息
        ScfHomePage dyCj = scfHomePageService.selectDyCj(orgCode.split(",") );
        if (dyCj != null) {
            // 设置当月成交金额
            scfHomePage.setDyTotalAmount(dyCj.getDyTotalAmount());
            // 设置当月成交吨数--单位换算kg-万吨
            scfHomePage.setDydsWeight(unitConverter.convertToMetricTons(dyCj.getDydsWeight()));
            // 设置当月减少碳排--单位换算kg-万吨
            scfHomePage.setDyCarbonEmissions(unitConverter.convertToMetricTons(dyCj.getDyCarbonEmissions()));
        }
        // 近一年平台成交金额及纳税
        ScfHomePage dnCj = scfHomePageService.selectDnCj(orgCode.split(","));
        if (dnCj != null) {
            scfHomePage.setDnTotalAmount(dnCj.getDnTotalAmount());
            scfHomePage.setDnnsAmount(dnCj.getDnnsAmount());
        } else {
            scfHomePage.setDnTotalAmount(new BigDecimal("0"));
            scfHomePage.setDnnsAmount(new BigDecimal("0"));
        }
        // 累计平台成交金额及纳税
        ScfHomePage ljCj = scfHomePageService.selectLjCj(orgCode.split(","));
        if (ljCj != null) {
            scfHomePage.setLjTotalAmount(ljCj.getLjTotalAmount());
            scfHomePage.setLjnsAmount(ljCj.getLjnsAmount());
        }
        // 近一年订单成交量
        String dnSalesCount = scfHomePageService.selectDnSalesCount(orgCode.split(",") ).getDnSalesCount();
        scfHomePage.setDnSalesCount(dnSalesCount);
        // 近一年成交吨数
        ScfHomePage homePage1 = scfHomePageService.selectDnWeight(orgCode.split(",") );
        if (homePage1 != null) {
            scfHomePage.setDnWeight(unitConverter.convertToMetricTons(homePage1.getDnWeight()));
        } else {
            scfHomePage.setDnWeight(new BigDecimal(0));
        }
        // 累计订单成交量
        ScfHomePage homePage2 = scfHomePageService.selectLjSalesCount(orgCode.split(",") );
        if (homePage2 != null) {
            String ljSalesCount = homePage2.getLjSalesCount();
            scfHomePage.setLjSalesCount(ljSalesCount);
        } else {
            scfHomePage.setLjSalesCount("0");
        }
        // 累计成交吨数
        ScfHomePage homePage3 = scfHomePageService.selectLjWeight( orgCode.split(",")  );
        if (homePage3 != null) {
            scfHomePage.setLjWeight(unitConverter.convertToMetricTons(homePage3.getLjWeight()));
        } else {
            scfHomePage.setLjWeight(new BigDecimal(0));
        }
        // 累计碳排放
        ScfHomePage homePage4 = scfHomePageService.selectCarbonEmissions( orgCode.split(",")  );
        if (homePage4 != null) {
            scfHomePage.setCarbonEmissions(unitConverter.convertToMetricTons(homePage4.getCarbonEmissions()));
        } else {
            scfHomePage.setCarbonEmissions(new BigDecimal(0));
        }
        return R.data(scfHomePage);
    }

    /** 近一个月成交信息
     * 近半年成交信息
     * 查询各月份主体数量
     *
     * @return
     */
    @GetMapping("/scfGetSubjectCountByMonth")
    // @Cacheable(value = "subjectCountByMonthCache", key = "'SubjectCountByMonth'")
    public R<List<ScfSubjectCount>> selectSubjectCountByMonth(String orgCode) {

        if (StringUtils.isEmpty(   orgCode) ) {
            return  R.fail(400,"公司编码不能为空") ;
        }

        List<ScfSubjectCount> scfSubjectCounts = scfHomePageService.selectSubjectCountByMonth(orgCode.split(","));
        // 获取主体总数
        String subjectCountAll = scfHomePageService.selectSubjectCounts(orgCode.split(","));
        // 遍历数组
        for (ScfSubjectCount subjectCount : scfSubjectCounts) {
            // 在这里执行对每个元素的操作
            // 给每条记录添加上主体总数
            subjectCount.setSubjectCount(subjectCountAll);
            // 添加更多你需要执行的操作
        }
        return R.data(scfSubjectCounts);
    }

    /**
     * 地图信息查询
     *
     * @return
     */
    @GetMapping("/scfGetAddressMapInfo")
    // @Cacheable(value = "addressMapInfoCache", key = "'AddressMapInfo'")
    public R<List<ScfSubjectAddress>> selectAddressMap(String  orgCode) {
        if (StringUtils.isEmpty(   orgCode) ) {
            return  R.fail(400,"公司编码不能为空") ;
        }

        ScfLineChart scfLineChart = scfHomePageService.selectLineChart( orgCode.split(","));



        return R.data(scfHomePageService.selectSubjectAddress(orgCode.split(",")));
    }

    /**
     * 商品成交排名列表查询
     *
     * @return
     */
    @GetMapping("/scfSelGoodsDealHomeList")
    // @Cacheable(value = "goodsDealHomeListCache", key = "'GoodsDealHomeList'")
    public R<IPage<ScfGoodsWeightCount>> selectGoodsWeightCount(Query query, ScfGoodsWeightCount scfGoodsWeightCount) {
        if (StringUtils.isEmpty( scfGoodsWeightCount.getOrgCode() ) ) {
            return  R.fail(400,"公司编码不能为空") ;
        }
        scfGoodsWeightCount.setOrgCodes(  scfGoodsWeightCount.getOrgCode().split(",") );

        if(scfGoodsWeightCount.getOrgCode().contains( "50000000")){
            scfGoodsWeightCount.setOrgCode("50000000");
            scfGoodsWeightCount.setOrgCodes("50000000".split(",") );
        }
        return R.data(scfHomePageService.selectGoodsWeightCount(Condition.getPage(query), scfGoodsWeightCount));
    }

    /**
     * 实时成交排名
     *
     * @return
     */
    @GetMapping("/scfSelRealtimeDealHomeList")
    // @Cacheable(value = "realtimeDealHomeListCache", key = "'RealtimeDealHomeList'")
    public R<IPage<ScfSelRealtime>> selectSelRealtime(Query query,ScfSelRealtime scfSelRealtime) {
        if (StringUtils.isEmpty( scfSelRealtime.getOrgCode() ) ) {
            return  R.fail(400,"公司编码不能为空") ;
        }
        scfSelRealtime.setOrgCodes(  scfSelRealtime.getOrgCode().split(",") );
        scfSelRealtime.setOwnerName( scfSelRealtime.getNameA() );
        scfSelRealtime.setRecyclingName( scfSelRealtime.getNameB());
        return R.data(scfHomePageService.selectSelRealtime(Condition.getPage(query), scfSelRealtime));
    }

    /**
     * 再生资源企业成交排名
     *
     * @return
     */
    @GetMapping("/scfSelRenewableDealHomeList")
    // @Cacheable(value = "renewableDealHomeListCache", key = "'RenewableDealHomeList'")
    public R<IPage<ScfSelRenewableRanking>> selectSelRenewableRanking(Query query, ScfSelRenewableRanking scfSelRenewableRanking) {
        if (StringUtils.isEmpty( scfSelRenewableRanking.getOrgCode() ) ) {
            return  R.fail(400,"公司编码不能为空") ;
        }
        scfSelRenewableRanking.setOrgCodes(  scfSelRenewableRanking.getOrgCode().split(",") );

        if(scfSelRenewableRanking.getOrgCode().contains( "50000000")){
            scfSelRenewableRanking.setOrgCode("50000000");
            scfSelRenewableRanking.setOrgCodes("50000000".split(",") );
        }
        return R.data(scfHomePageService.selectSelRenewableRanking(Condition.getPage(query), scfSelRenewableRanking));
    }

    /**
     * 利废企业成交排名
     *
     * @return
     */
    @GetMapping("/scfSelRecyclelingDealHomeList")
    // @Cacheable(value = "recyclelingDealHomeListCache", key = "'RecyclelingDealHomeList'")
    public R<IPage<ScfSelRecyclelingRanking>> selectSelRecyclelingRanking(Query query, ScfSelRecyclelingRanking scfSelRecyclelingRanking) {
        if (StringUtils.isEmpty( scfSelRecyclelingRanking.getOrgCode() ) ) {
            return  R.fail(400,"公司编码不能为空") ;
        }
        scfSelRecyclelingRanking.setOrgCodes(  scfSelRecyclelingRanking.getOrgCode().split(",") );


        if(scfSelRecyclelingRanking.getOrgCode().contains( "50000000")){
            scfSelRecyclelingRanking.setOrgCode("50000000");
            scfSelRecyclelingRanking.setOrgCodes("50000000".split(",") );
        }

        return R.data(scfHomePageService.selectSelRecyclelingRanking(Condition.getPage(query), scfSelRecyclelingRanking));
    }

    /**
     * 供应商成交排名
     *
     * @return
     */
    @GetMapping("/scfSelOwnerDealHomeList")
    // @Cacheable(value = "recyclelingDealHomeListCache", key = "'RecyclelingDealHomeList'")
    public R<IPage<ScfSelOwnerRanking>> selectSelOwnerRanking(Query query, ScfSelOwnerRanking scfSelOwnerRanking) {
        if (StringUtils.isEmpty( scfSelOwnerRanking.getOrgCode() ) ) {
            return  R.fail(400,"公司编码不能为空") ;
        }
        scfSelOwnerRanking.setOrgCodes(  scfSelOwnerRanking.getOrgCode().split(",") );

        if(scfSelOwnerRanking.getOrgCode().contains( "50000000")){
            scfSelOwnerRanking.setOrgCode("50000000");
            scfSelOwnerRanking.setOrgCodes("50000000".split(",") );
        }

        return R.data(scfHomePageService.selectSelOwnerRanking(Condition.getPage(query), scfSelOwnerRanking));
    }

    /**
     * 查询折线图数据
     *月度成交趋势图
     * @return
     */
    @GetMapping("/scfGetLineChartInfo")
    // @Cacheable(value = "lineChartInfoCache", key = "'LineChartInfo'")
    public R<ScfLineChart> selectLineChartInfo(String orgCode) {
        if (StringUtils.isEmpty(   orgCode) ) {
            return  R.fail(400,"公司编码不能为空") ;
        }

        ScfLineChart scfLineChart = scfHomePageService.selectLineChart( orgCode.split(","));
        List<ScfLineChartSection> scfLineChartSections = scfHomePageService.selectLineChartSection(orgCode.split(","));
        for (ScfLineChartSection scfLineChartSection : scfLineChartSections) {
            scfLineChartSection.setTotalTons(scfLineChartSection.getTotalTons().divide(BigDecimal.valueOf(1000), 2, RoundingMode.HALF_UP));
        }
        scfLineChart.setScfLineChartSectionList(scfLineChartSections);
        return R.data(scfLineChart);
    }



}
